package com.txunda.zhuohired.aty;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ants.theantsgo.config.Config;
import com.flyco.tablayout.utils.FragmentChangeManager;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.fgt.user.FindFgt;
import com.txunda.zhuohired.fgt.user.HomeFgt;
import com.txunda.zhuohired.fgt.user.MyFgt;
import com.txunda.zhuohired.fgt.user.NewsFgt;
import com.txunda.zhuohired.fgt.user.ResumeFgt;

import java.util.ArrayList;

public class MainAty extends BaseAty implements RadioGroup.OnCheckedChangeListener {
    /**
     * 测试
     */
    @ViewInject(R.id.rg_home)
    private RadioGroup rg_home;
    /**
     * 首页
     */
    @ViewInject(R.id.rb_home)
    private RadioButton rb_home;
    /**
     * 消息
     */
    @ViewInject(R.id.rb_news)
    private RadioButton rb_news;
    /**
     * 发现
     */
    @ViewInject(R.id.rb_find)
    private RadioButton rb_find;
    /**
     * 简历
     */
    @ViewInject(R.id.rb_resume)
    private RadioButton rb_resume;
    /**
     * 我的
     */
    @ViewInject(R.id.rb_my)
    private RadioButton rb_my;

    private FragmentChangeManager fcm;
    private ArrayList<Fragment> fragments;
    private int checkStatus = 0;
    private HomeFgt homeFgt;
    private NewsFgt newsFgt;
    private FindFgt findFgt;
    private ResumeFgt resumeFgt;
    private MyFgt myFgt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rg_home.setOnCheckedChangeListener(this);
        fcm = new FragmentChangeManager(getSupportFragmentManager(), R.id.fl_home, fragments);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_main;
    }

    @Override
    protected void initialized() {
        fragments = new ArrayList<>();
        homeFgt = new HomeFgt();
        newsFgt = new NewsFgt();
        findFgt = new FindFgt();
        resumeFgt = new ResumeFgt();
        myFgt = new MyFgt();
        fragments.add(homeFgt);
        fragments.add(newsFgt);
        fragments.add(findFgt);
        fragments.add(resumeFgt);
        fragments.add(myFgt);
    }

    @Override
    protected void requestData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (Config.isLogin()) {// 已登录
            switch (checkStatus) {
                case 0:
                    rb_home.setChecked(true);
                    break;
                case 1:
                    rb_news.setChecked(true);
                    break;
                case 2:
                    rb_find.setChecked(true);
                    break;
                case 3:
                    rb_resume.setChecked(true);
                    break;
                case 4:
                    rb_my.setChecked(true);
                    break;
            }
        } else {// 未登录
            switch (checkStatus) {
                case 0:
                    rb_home.setChecked(true);
                    break;
                case 1:
                    rb_news.setChecked(true);
                    break;
                case 2:
                    rb_find.setChecked(true);
                    break;
                case 3:
                    rb_resume.setChecked(true);
                    break;
                case 4:
                    rb_my.setChecked(true);
                    break;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.rb_home://首页
                checkStatus = 0;
                fcm.setFragments(0);
                break;
            case R.id.rb_news://消息
                checkStatus = 1;
                fcm.setFragments(1);
                break;
            case R.id.rb_find://发现
                checkStatus = 2;
                fcm.setFragments(2);
                break;
            case R.id.rb_resume://简历
                checkStatus = 3;
                fcm.setFragments(3);
                break;
            case R.id.rb_my://我的
                checkStatus = 4;
                fcm.setFragments(4);
                break;
        }
    }
}
